<?php

namespace app\controllers;

use app\models\RedisCache;
use Yii;
use yii\web\Controller;
use app\models\LoginForm;
use app\models\HistoryLogin;
class DefaultController extends Controller
{
    public $layout = false;

    public function actions()
    {
        return [
            'captcha' => [
                'class' => 'app\models\actions\CaptchaAction',
                'minLength' => 4,
                'maxLength' => 4,
                'transparent' => true,
                'offset' => 15,
                'foreColor' => 0x04a9f4,
                'backColor' => 0x000000,
                'height' => 50
            ],
        ];
    }
    public function actionIndex()
    {
        $type = 0;
        $address='';
        $req = Yii::$app->request;
        $str = base64_decode($req->get('key'));
        if ($str){
            $list = explode('&',$str);
            for ($i=0;$i<count($list);$i++){
                $data = explode('=',$list[$i]);
                if ($data[0]=='uid'){
                    $uid = $data[1];
                }else if ($data[0]=='type'){
                    if ($data[1]==0){
                        return "走错地址了！";
                    }
                    $type=$data[1];

                }
            }
         $name = 'address-'.$uid;
         $res = Yii::$app->cache->get($name);
         if ($res['type']==0 && $res['code']==1){
             $data = [
                 'code' => 0,
                 'msg' =>'有点尴尬，你好像到期了！'
             ];
         }else{
             if ($type==1){
                 $address = ltrim($res['app_address'],'./');
             }else if ($type==2){
                 $address = ltrim($res['ios_address'],'./');
             }
             $data = [
                 'code' => 1,
                 'user'=>$res['user'],
                 'img' => $res['img_address'],
                 'address'=>$address
                 ];
         }
        }else{
            $data = [
                'code' => 0,
                'msg' =>'请输入正确的地址！'
            ];
        }
//           return $this->redirect(array('/default/friendly'));
        return $this->render('index',['data'=>$data]);
    }

    public function actionError(){
        echo "请联系管理维护！";exit();
    }

    public function actionAdmin()
    {
        return $this->render('admin');
    }

    public function actionSubmit(){
       $req =  Yii::$app->request;
       if ($req->isPost){
           $username =trim(\yii\helpers\Html::encode($req->post('username')));
           $password =trim(\yii\helpers\Html::encode($req->post('password')));
           $captcha =trim(\yii\helpers\Html::encode($req->post('captcha')));
           $model = new LoginForm();
           $model->username = $username;
           $model->password = $password;
           $model->captcha = $captcha;
           if($model->validate()){
               //记录到登录表中
               $loginObj = new HistoryLogin();
               $user =  $username;
               $type = 3;
               $time = time();
               $ip = $req->userIP;
               $ua = $req->userAgent;

               $loginObj->attributes = [
                   'username' => $user,
                   'type' => $type,
                   'time' => $time,
                   'ip' => $ip,
                   'ua' => $ua,
               ];
               $loginObj->save();
               $pass_auto = md5(md5($username) . Yii::$app->params['pass_auto']);
               //登录的账户名保存到cookie中
               setcookie("$username", md5($pass_auto), time() + 3600, '/');

               //跳转
               exit(json_encode(['code' => 1, 'msg' => '登录成功']));
           } else {
               $errors = $model->getErrors();
               foreach ($errors as $error) {
                   foreach ($error as $v) {
                       exit(json_encode(['code' => 0, 'msg' => $v]));
                   }
               }
           }
       }else{
           return 404;
       }
    }
     public function actionLogout(){
        if (isset($_COOKIE['admin'])){
            setcookie("admin", "", time()-3600,'/');
        }
        exit(json_encode(['code' => 1, 'msg' => '']));
     }

     /*
      * 暂时前端显示页面
      */
     public function actionNs() {
         $req = Yii::$app->request;
         $key = $req->get('key');
         if (!empty($key)){
             $str = RedisCache::can($key);
             if (time()<strtotime($str['time'])){
                 $str['code'] = 1;
             }else{
                 $str['code'] = 0;
             }
             return $this->render('ns',['str'=>$str]);
         }else{
             echo "<script>window.location.href='/';</script>";
         }
     }
}
